package cn.edu.njuit.webserver.securityserver.orders;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Random;

/**
 * @Description: TODO
 * @Data: 2022/9/19 17:49
 * @Author: guzy
 */
@Service
public class ShopOrderServiceImpl implements ShopOrderService {
    private Log logger = LogFactory.getLog("商户数据服务");
    private final OrderRepository orderRepository;

    @Resource
    OrderQueueService orderQueueService;

    @Resource
    MakingService makingService;

    public ShopOrderServiceImpl(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    /**
     * 队列1消息消费者：店家自动接单
     *
     * @param message
     */
    @RabbitListener(queues = "queue.order.confirm")
    public void listenOrderConfirm(String message) {
        logger.info(String.format("收到待确认订单：%s", message));
        //确认订单并生产
        OrderEntity orderEntity = this.confirmOrder(message);
    }

    /**
     * 队列3消息消费者：店家自动配送
     * @param message
     */
    @RabbitListener(queues = "queue.shop.delive")
    public void listenShopDelive(String message){
        logger.info(String.format("收到无骑手配送订单，由店家安排接单：%s",message));
        //确认订单并生产
        OrderEntity orderEntity=this.finishDeliver(message);
    }

    @Override
    public OrderEntity confirmOrder(String orderCode) {
        OrderEntity orderEntity = this.orderRepository.findByOrderCode(orderCode);
        if (orderEntity != null) {
            //标记订单为制作中状态
            orderEntity.setOrderStatus(OrderStatus.MAKING.getName());
            orderEntity.setOrderConfirmTime(LocalDateTime.now());
            orderEntity.setOrderShopName("有单统统接的门店");
            orderEntity = this.orderRepository.save(orderEntity);
            this.logger.info(String.format("订单已确认：%s", orderCode));

            //订单确认后，直接进入制作环节
//            orderEntity = this.processOrder(orderCode);
            orderEntity = this.makingService.processOrder(orderCode);

            return orderEntity;
        }
        return null;
    }

    @Override
    public OrderEntity processOrder(String orderCode) {
        OrderEntity orderEntity = this.orderRepository.findByOrderCode(orderCode);
        if (orderEntity != null) {
            this.logger.info(String.format("订单开始制作：%s", orderCode));
            Random r = new Random();
            Integer delay = 10 + r.nextInt(30);
            //模拟订单制作过程，设置10~40秒随机时间等待
            try {
                Thread.sleep(delay * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //订单制作完成标注为待配送状态
            orderEntity.setOrderStatus(OrderStatus.WAITINGRIDER.getName());
            orderEntity.setOrderMadeTime(LocalDateTime.now());
            orderEntity = this.orderRepository.save(orderEntity);
            this.logger.info(String.format("订单制作完成，进入配送状态:%s", orderCode));

            //添加以下代码，生产完成发送待配送消息入队列2
            this.orderQueueService.sendOrderDeliveMessage(orderEntity.getOrderCode());
            return orderEntity;
        }
        return null;
    }

    @Override
    public OrderEntity finishDeliver(String orderCode) {
        OrderEntity orderEntity = this.orderRepository.findByOrderCode(orderCode);
        if (orderEntity != null) {
            Random r = new Random();
            Integer delay = 10 + r.nextInt(30);
            //模拟订单配送过程，设置10~40秒随机时间等待
            try {
                Thread.sleep(delay * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            orderEntity.setOrderDeliverName("商家配送");
            orderEntity.setOrderDeliverCompleteTime(LocalDateTime.now());
            orderEntity.setOrderStatus(OrderStatus.DELIVEFINISHED.getName());
            orderEntity = this.orderRepository.save(orderEntity);
            logger.info(String.format("店家配送完成：%s", orderCode));

            //添加以下代码，配送完成后加入队列4，已完成队列
            this.orderQueueService.sendCompleteMessage(orderEntity.getOrderCode());
            return orderEntity;
        }
        return null;
    }

}
